﻿using Customer.Domain;
using Customer.Domain.Dto;
using Customer.Infrstructre.FinanceManager;
using Customer.Read.API.Applcation.Command.FinanceTypeCommand;
using MediatR;
using Microsoft.IdentityModel.Tokens;

namespace Customer.Read.API.Applcation.CommandHandler.FinanceTypeCommandHandler
{
    /// <summary>
    /// 显示应收管理列表
    /// </summary>
    public class SelectReceivableCommandHandler:IRequestHandler<SelectReceivableCommand, Receivablepage>
    {
        private readonly IReceivableRepository _receRep;//应收表
        private readonly IFinanceTypeRepository _financeTypeRep;//财务类别
        private readonly IPaymentRepository _paymentRep;//实收表

        public SelectReceivableCommandHandler(IReceivableRepository receRep, IFinanceTypeRepository financeTypeRep, IPaymentRepository paymentRep)
        {
            _receRep = receRep;
            _financeTypeRep = financeTypeRep;
            _paymentRep = paymentRep;
        }

        public async Task<Receivablepage> Handle(SelectReceivableCommand request, CancellationToken cancellationToken)
        {
            //var res=(await _receRep.GetAllAsync()).Where(x=>x.Office_Id==request.Office_Id && x.IsDel==false);
            //var res = from type in await _financeTypeRep.GetAllAsync()
            //          join rece in await _receRep.GetAllAsync()
            //          on type.FinanceTypeId equals rece.FinanceTypeId
            //          join pay in await _paymentRep.GetAllAsync()
            //          on rece.SerialNumber equals pay.SerialNumber
            //          where rece.Office_Id == request.Office_Id && rece.IsDel == false
            //          select new ReceDTO
            //          {
            //              ReceivableId=rece.ReceivableId,
            //              SerialNumber=rece.SerialNumber,
            //              ReceivableName=rece.ReceivableName,
            //              FinanceTypeId=rece.FinanceTypeId,
            //              Lastpaydate=rece.Lastpaydate,
            //              Payer=rece.Payer,
            //              Beneficiary=rece.Beneficiary,
            //              ReceivableAmount=rece.ReceivableAmount,
            //              PaymentNum=rece.PaymentNum,
            //              ReceivableState=rece.ReceivableState,
            //              RRremark=rece.RRremark,
            //              Office_Id=rece.Office_Id,
            //              User_Id=rece.User_Id,
            //              FinanceTypeName=type.FinanceTypeName,
            //              PaymentPrice=pay.PaymentPrice,
            //              AddName = rece.AddName,
            //              AddTime = rece.AddTime,
            //              UpdName = rece.UpdName,
            //              UpdTime = rece.UpdTime,
            //              DelName=rece.DelName,
            //              DelTime=rece.DelTime
            //          };
            var res = from type in await _financeTypeRep.GetAllAsync()
                      join rece in await _receRep.GetAllAsync()
                      on type.FinanceTypeId equals rece.FinanceTypeId
                      where rece.Office_Id == request.Office_Id && rece.IsDel == false
                      select new ReceDTO
                      {
                          ReceivableId = rece.ReceivableId,
                          SerialNumber = rece.SerialNumber,
                          ReceivableName = rece.ReceivableName,
                          FinanceTypeId = rece.FinanceTypeId,
                          Lastpaydate = rece.Lastpaydate,
                          Payer = rece.Payer,
                          Beneficiary = rece.Beneficiary,
                          ReceivableAmount = rece.ReceivableAmount,
                          PaymentNum = rece.PaymentNum,
                          ReceivableState = rece.ReceivableState,
                          RRremark = rece.RRremark,
                          Office_Id = rece.Office_Id,
                          User_Id = rece.User_Id,
                          FinanceTypeName = type.FinanceTypeName,
                          PaymentPrice = rece.PaymentPrice,
                          ResidueMoney =rece.ResidueMoney,
                          AddName = rece.AddName,
                          AddTime = rece.AddTime,
                          UpdName = rece.UpdName,
                          UpdTime = rece.UpdTime,
                          DelName = rece.DelName,
                          DelTime = rece.DelTime
                      };
            //流水号
            if (!string.IsNullOrEmpty(request.SerialNumber))
            {
                res=res.Where(x=>x.SerialNumber==request.SerialNumber);
            }
            //名称
            if (!string.IsNullOrEmpty(request.ReceivableName))
            {
                res = res.Where(x => x.ReceivableName .Contains(request.ReceivableName));
            }
            //财务类别
            if (request.FinanceTypeId>0)
            {
                res = res.Where(x => x.FinanceTypeId == request.FinanceTypeId);
            }
            //付款方
            if (!string.IsNullOrEmpty(request.Payer))
            {
                res = res.Where(x => x.Payer == request.Payer);
            }
            //起始时间
            if (!string.IsNullOrEmpty(request.Kdate))
            {
                res = res.Where(x => x.AddTime >= Convert.ToDateTime(request.Kdate));
            }
            //结束时间
            if (!string.IsNullOrEmpty(request.Sdate))
            {
                res = res.Where(x => x.AddTime <= Convert.ToDateTime(request.Sdate));
            }
            //状态
            if (request.ReceivableState>0)
            {
                res = res.Where(x => x.ReceivableState == request.ReceivableState);
            }
            Receivablepage page = new Receivablepage();
            page.sumcount = res.Count();
            page.lastpage = (int)Math.Ceiling(page.sumcount * 1.0 / request.pagesize);
            page.list = res.ToList().Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize).ToList();
            return page;

        }
    }
}
